import json
from robot.api import logger

from rediscluster import RedisCluster
from Lib.zcm.assistant import FwVariables


def redis_connect(startup_nodes=None, decode_responses=True, link_name="redis", **kwargs):
    if not startup_nodes:
        # 用户未通过传参传入，则到config.ini中读取
        startup_nodes = FwVariables.get_startup_nodes(link_name)
        startup_nodes = json.loads(startup_nodes)
    logger.info("startup_nodes : {}".format(startup_nodes))
    return RedisCluster(startup_nodes=startup_nodes, decode_responses=decode_responses, **kwargs)


def redis_disconnect(rc):
    rc.connection_pool.disconnect()


if __name__ == '__main__':
    startup_nodes = [{"host": "10.45.50.144", "port": "8686"}, {"host": "10.45.50.144", "port": "8687"},
                     {"host": "10.45.50.144", "port": "8688"}]
    rc = RedisCluster(startup_nodes=startup_nodes, decode_responses=True)
    rc.connection_pool.disconnect()
    # rc.set("dev:OSSR9.0:FM:FM:RULE:FORWARD:MVEL:foo", "bar")
    # print(rc.hgetall("dev:OSSR9.0:FM:FM:RULE:COMPRESSION:MVEL"))
    func = getattr(rc, "hget")
    print(func("dev:OSSR9.0:FM:FM:RULE:COMPRESSION:MVEL", "1000010921042617110000000007"))
